﻿//***************************************************
//
//       File Name:    TouchMoudel.cs
//
//       Author:   王文军    Email:  546527816@qq.com
//
//
//***************************************************

using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public static class TouchMoudel {

    //缩放最大最小值
    public static float minimumDistance = 0;
    public static float maximumDistance = 200;
    public static float distance;
    public static float distance1;
    public static float lastDist;
    public static float curDist;
    public static RaycastHit hit;
    public static Ray ray;
    public static Touch touch;

    //拖动范围
    public static float minPosX = -900f;
    public static float maxPosX = 1000f;
    public static float minPosY = -350f;
    public static float maxPosY = 300f;

    public static bool isKeyDown = false;
    private static float mouse2;

    public static void TouchManager(Camera uiCamera, Transform cheShen,  Vector3 myLocalScale,VehicleInfo vInfo)
    {
        //if (Input.GetKeyDown(KeyCode.Escape))
        //    Application.Quit();
        if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
        {
             ray = uiCamera.ScreenPointToRay(Input.GetTouch(0).position);
            if (Physics.Raycast(ray, out hit))
            {
                touch = Input.GetTouch(0);
                if (hit.collider.tag == StringManager.MoudleOBJTag)
                {
                    //在一定范围里旋转
                    if (cheShen.eulerAngles.y < vInfo.eulerAngles2 && cheShen.eulerAngles.y > vInfo.eulerAngles1)
                        vInfo.y -= touch.deltaPosition.y * vInfo.xSpeed;//  * 0.02f* xSpeed
                    if (cheShen.eulerAngles.y < vInfo.eulerAngles4 && cheShen.eulerAngles.y > vInfo.eulerAngles3)
                        vInfo.y += touch.deltaPosition.y * vInfo.xSpeed;//  * 0.02f* xSpeed
                    vInfo.x -= touch.deltaPosition.x;//  * 0.02f* ySpeed 
                    Vector3 vt = new Vector3(touch.deltaPosition.x * 0.02f, touch.deltaPosition.y * 0.02f, 0);

                    //if (distance > 30)
                    //{
                    //    //触摸拖动
                    //    cheShen.position += vt;
                    //    Vector3 cheShenPos = cheShen.localPosition;
                    //    cheShenPos.x = Mathf.Clamp(cheShenPos.x, minPosX, maxPosX);
                    //    cheShenPos.y = Mathf.Clamp(cheShenPos.y, minPosY, maxPosY);
                    //    cheShen.transform.localPosition = cheShenPos;
                    //}
                    Quaternion rotation = Quaternion.Euler(vInfo.z, vInfo.x, vInfo.y);
                        cheShen.rotation = rotation;
                }
            }
        }
        if (Input.touchCount > 1 && (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved))
        {
            ray = uiCamera.ScreenPointToRay(Input.GetTouch(0).position);
            if (Physics.Raycast(ray, out hit))
            {
                if (hit.collider.tag == StringManager.MoudleOBJTag)
                {
                    var touch1 = Input.GetTouch(0);
                    var touch2 = Input.GetTouch(1);
                    curDist = Vector2.Distance(touch1.position, touch2.position);
                    if (curDist > lastDist)
                        distance += Vector2.Distance(touch1.deltaPosition, touch2.deltaPosition); //* pinchSpeed / 10;
                    else {
                        distance -= Vector2.Distance(touch1.deltaPosition, touch2.deltaPosition); // * pinchSpeed / 10;
                    }
                    lastDist = curDist;
                    if (distance <= minimumDistance)
                        distance = minimumDistance;
                    if (distance >= maximumDistance)
                        distance = maximumDistance;
                    distance1 = distance * (vInfo.pinchSpeed / 10);
                    Vector3 position = new Vector3(distance1, distance1, distance1);
                    //缩放
                    cheShen.localScale = position + myLocalScale;
                }
            }
        }



        mouse2 = Input.GetAxis("Mouse ScrollWheel");
        if (mouse2 != 0 && Input.touchCount <= 0)
        {
            ray = uiCamera.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))
            {
                if (hit.collider.tag == StringManager.MoudleOBJTag)
                {
                    distance += mouse2 * 50;
                    if (distance <= minimumDistance)
                        distance = minimumDistance;
                    if (distance >= maximumDistance)
                        distance = maximumDistance;
                    distance1 = distance * (vInfo.pinchSpeed / 10);
                    Vector3 position = new Vector3(distance1, distance1, distance1);
                    //缩放
                    cheShen.localScale = position + myLocalScale;
                }
            }
        }
        
        if (Input.GetMouseButtonDown(0))
            isKeyDown = true;
        if (Input.GetMouseButtonUp(0))
            isKeyDown = false;
        if (isKeyDown == true&& Input.touchCount <= 0)
        {
            ray = uiCamera.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))
            {
                if (hit.collider.tag == StringManager.MoudleOBJTag)
                {
                    //在一定范围里旋转
                    if (cheShen.eulerAngles.y < vInfo.eulerAngles2 && cheShen.eulerAngles.y > vInfo.eulerAngles1)
                        vInfo.y -= Input.GetAxis("Mouse Y") * 30;//  * 0.02f* xSpeed
                    if (cheShen.eulerAngles.y < vInfo.eulerAngles4 && cheShen.eulerAngles.y > vInfo.eulerAngles3)
                        vInfo.y += Input.GetAxis("Mouse Y") * 30;//  * 0.02f* xSpeed
                    vInfo.x -= Input.GetAxis("Mouse X") * 30;//  * 0.02f* ySpeed 
                    Vector3 vt = new Vector3(Input.GetAxis("Mouse X") * 0.02f, Input.GetAxis("Mouse X") * 0.02f, 0);

                    //if (distance > 30)Mouse X
                    //{
                    //    //触摸拖动
                    //    cheShen.position += vt;
                    //    Vector3 cheShenPos = cheShen.localPosition;
                    //    cheShenPos.x = Mathf.Clamp(cheShenPos.x, minPosX, maxPosX);
                    //    cheShenPos.y = Mathf.Clamp(cheShenPos.y, minPosY, maxPosY);
                    //    cheShen.transform.localPosition = cheShenPos;
                    //}
                    Quaternion rotation = Quaternion.Euler(vInfo.z, vInfo.x,vInfo.y );
                        cheShen.rotation = rotation;
                }
            }
        }
    }

   

}
